import { getResource } from "@/api/resource";
import {
  repairFindProblem,
  repairFindEqpt,
  repairFindMemberPeopel,
} from "@/api/maintenance/repairacceptancemanagement/services";
import { computed, ref } from "vue";
import _ from "lodash";

export const terminalData: any = ref([]);
export const eqptFloorData: any = ref([]);
export const repairFindEqptData: any = ref([]);
export const repairFindProblemData: any = ref([]);
export const createUserOrgIdData: any = ref([]);
export const memberPeopelData: any = ref([]);
const terminalAndEqptFloorData: any = ref([]);

/*
    获取航站楼和楼层
  */
export const getTerminalAndUrgency = async () => {
  const terminalDataArr: any = [];
  const res = await getResource("terminal");
  res.data.properties.meta.forEach((item: any) => {
    terminalDataArr.push({
      label: item.label + "航站楼" + item.children.length,
      value: item.value,
    });
 
   
    
    eqptFloorData.value=res.data.properties.meta[0].children
  });
  terminalAndEqptFloorData.value = res.data.properties.meta;
  terminalData.value = terminalDataArr;
};

// export const getEqptFloorData = (value: any) => {
//   const listArr: { label: any; value: any }[] = [];
//   const allListArr: { label: any; value: any }[] = [];
//   if (value.length === 0) return (eqptFloorData.value = []);
//   if (value.length == 1) {
//     const res = terminalAndEqptFloorData.value.find(
//       (item: any) => item.value == value[0]
//     );
//     res.children.map((item: any) => {
//       listArr.push({
//         label: item.label,
//         value: item.value,
//       });
//     });
//     return (eqptFloorData.value = listArr);
//   }

//   terminalAndEqptFloorData.value.map((item: any) => {
//     item.children.map((item: any) => {
//       allListArr.push({
//         label: item.label,
//         value: item.value,
//       });
//     });
//   });
//   return (eqptFloorData.value = allListArr);
// };

export const getRepairFindProblem = async (value: string) => {
  const listArr: any[] = [];
  if(value=='0'){
    value=''
  }
  const res = await repairFindProblem({ typeId: value });
  res.data.map((item: any) => {
    listArr.push({
      label: item.problemType,
      value: item.problemId,
    });
  });
  repairFindProblemData.value = listArr;
};

const getTreeData = (arr: any) => {
  const tree: any = [];
  arr.forEach((item: any) => {
    const node = {
      label: item.typeName,
      value: item.typeId,
      children: getTreeData(item.children),
    };
    tree.push(node);
  });
  return tree;
};

export const getRepairFindEqpt = async () => {
  const res = await repairFindEqpt({});
  const listArr = getTreeData(res.data);
  repairFindEqptData.value = [...listArr, { label: "其他", value: "0" }];
};

const getOrgTreeData = (arr: any) => {
  const tree: any = [];
  arr.forEach((item: any) => {
    const node = {
      label: item.data.name,
      value: item.data.organId,
      children: getOrgTreeData(item.children),
    };
    tree.push(node);
  });
  return tree;
};

export const getCreateUserOrgIdData = (arr: any) => {
  const listArr = getOrgTreeData(arr);
  createUserOrgIdData.value = listArr;
};

export const getRepairFindMemberPeopel = async () => {
  const listArr: any = [];
  const res = await repairFindMemberPeopel({});
  res.data.map((item: any) => {
    listArr.push({
      label: item.creator.name,
      value: item.creator.id,
    });
  });
const arr=_.uniqWith(listArr, _.isEqual)
//  除去名字为null的人员
  const index = arr.findIndex(item => item.label === null);
if (index !== -1) {
  arr.splice(index, 1);
  console.log(arr,'维修人员');
}
  memberPeopelData.value = arr;
};
